{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ArcGIS API for Python\n",
    "\n",
    "## How to install ArcGIS API for Python\n",
    "\n",
    "* ArcGIS API for Python only for Python 3\n",
    "* https://developers.arcgis.com/python/guide/install-and-set-up/#Using-the-API\n",
    "* Create a Python virtual environment using Anaconda: `conda create -n py36 python=3.6`\n",
    "* Activate the Python environment: `activate py36`\n",
    "* Install arcgis to the Python environment: `conda install -c esri arcgis`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from arcgis.gis import GIS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a84dd4592bd1429b82a590af91e8de57",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MapView(layout=Layout(height='400px', width='100%'))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "my_gis = GIS()\n",
    "my_gis.map()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<Item title:\"מיפוי שריפה חדש\" type:Feature Layer Collection owner:kkl_arcgis_10>,\n",
       " <Item title:\"Intact Forest Landscapes (2000)\" type:Feature Layer Collection owner:GlobalForestWatch>,\n",
       " <Item title:\"WRI_BeforeAndAfterPhotos\" type:Feature Layer Collection owner:FireFFSL>,\n",
       " <Item title:\"Social Vulnerability 2010\" type:Feature Layer Collection owner:AtlasPublisher>,\n",
       " <Item title:\"GIS_Export\" type:Feature Layer Collection owner:baszlera>]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from arcgis.gis import GIS\n",
    "gis = GIS()\n",
    "public_content = gis.content.search(\"Fire\", item_type=\"Feature Layer\", max_items=5)\n",
    "public_content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://www.arcgis.com/home/item.html?id=f867e79cccbd4ac1909e5883e323d0d7' target='_blank'>\n",
       "                        <img src='https://www.arcgis.com/sharing/rest//content/items/f867e79cccbd4ac1909e5883e323d0d7/info/thumbnail/ago_downloaded.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://www.arcgis.com/home/item.html?id=f867e79cccbd4ac1909e5883e323d0d7' target='_blank'><b>מיפוי שריפה חדש</b>\n",
       "                        </a>\n",
       "                        <br/>שכבה פוליגונלית של היקף שריפה חיה איתה עובדים באופן שוטף ומעדכנים בקולקטור<img src='https://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by kkl_arcgis_10\n",
       "                        <br/>Last Modified: November 15, 2018\n",
       "                        <br/>0 comments, 21,729 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"מיפוי שריפה חדש\" type:Feature Layer Collection owner:kkl_arcgis_10>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://www.arcgis.com/home/item.html?id=5a811508e1ab4ddbb79f7f0753d7d2d7' target='_blank'>\n",
       "                        <img src='https://www.arcgis.com/sharing/rest//content/items/5a811508e1ab4ddbb79f7f0753d7d2d7/info/thumbnail/ago_downloaded.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://www.arcgis.com/home/item.html?id=5a811508e1ab4ddbb79f7f0753d7d2d7' target='_blank'><b>Intact Forest Landscapes (2000)</b>\n",
       "                        </a>\n",
       "                        <br/>Identifies the world’s last remaining unfragmented forest landscapes, large enough to retain all native biodiversity and showing no signs of human alteration as of the year 2013. This layer also shows the reduction in the extent of Intact Forest Landscapes from 2000 to 2013.<img src='https://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by GlobalForestWatch\n",
       "                        <br/>Last Modified: October 29, 2018\n",
       "                        <br/>0 comments, 2,863 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Intact Forest Landscapes (2000)\" type:Feature Layer Collection owner:GlobalForestWatch>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://www.arcgis.com/home/item.html?id=8c79338b862843afb99923fe8572c939' target='_blank'>\n",
       "                        <img src='https://www.arcgis.com/sharing/rest//content/items/8c79338b862843afb99923fe8572c939/info/thumbnail/SkitzyAfter.jpg' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://www.arcgis.com/home/item.html?id=8c79338b862843afb99923fe8572c939' target='_blank'><b>WRI_BeforeAndAfterPhotos</b>\n",
       "                        </a>\n",
       "                        <br/>WRI Projects with Before and After Photos<img src='https://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by FireFFSL\n",
       "                        <br/>Last Modified: January 24, 2018\n",
       "                        <br/>0 comments, 9,409 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"WRI_BeforeAndAfterPhotos\" type:Feature Layer Collection owner:FireFFSL>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://www.arcgis.com/home/item.html?id=eee333118bd740268ba1cad0b78f83d6' target='_blank'>\n",
       "                        <img src='https://www.arcgis.com/sharing/rest//content/items/eee333118bd740268ba1cad0b78f83d6/info/thumbnail/ago_downloaded2.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://www.arcgis.com/home/item.html?id=eee333118bd740268ba1cad0b78f83d6' target='_blank'><b>Social Vulnerability 2010</b>\n",
       "                        </a>\n",
       "                        <br/>This map shows a simple summary of social vulnerability of populations in the United States.<img src='https://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by AtlasPublisher\n",
       "                        <br/>Last Modified: July 19, 2018\n",
       "                        <br/>0 comments, 195,382 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Social Vulnerability 2010\" type:Feature Layer Collection owner:AtlasPublisher>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://www.arcgis.com/home/item.html?id=fa5be57a918845158064c94b18929382' target='_blank'>\n",
       "                        <img src='https://www.arcgis.com/sharing/rest//content/items/fa5be57a918845158064c94b18929382/info/thumbnail/ago_downloaded.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://www.arcgis.com/home/item.html?id=fa5be57a918845158064c94b18929382' target='_blank'><b>GIS_Export</b>\n",
       "                        </a>\n",
       "                        <br/><img src='https://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by baszlera\n",
       "                        <br/>Last Modified: April 26, 2018\n",
       "                        <br/>0 comments, 518 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"GIS_Export\" type:Feature Layer Collection owner:baszlera>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.display import display\n",
    "for item in public_content:\n",
    "    display(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c559b3634c3c4d9d87d064d72cef9a96",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MapView(layout=Layout(height='400px', width='100%'))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "map1 = gis.map('Collier County, FL')\n",
    "map1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#get the first item\n",
    "fire = public_content[0]\n",
    "\n",
    "#add to map\n",
    "map1.add_layer(fire)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://www.arcgis.com/home/item.html?id=407c7fb33e784eca96a3bf345050c098' target='_blank'>\n",
       "                        <img src='https://www.arcgis.com/sharing/rest//content/items/407c7fb33e784eca96a3bf345050c098/info/thumbnail/ago_downloaded.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://www.arcgis.com/home/item.html?id=407c7fb33e784eca96a3bf345050c098' target='_blank'><b>Landsat_Multispectral</b>\n",
       "                        </a>\n",
       "                        <br/><img src='https://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/imagery16.png' style=\"vertical-align:middle;\">Imagery Layer by Craig_ESRIAU\n",
       "                        <br/>Last Modified: November 24, 2016\n",
       "                        <br/>0 comments, 90 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Landsat_Multispectral\" type:Imagery Layer owner:Craig_ESRIAU>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from arcgis.gis import GIS\n",
    "from arcgis.raster.functions import *\n",
    "gis = GIS()\n",
    "landsat_item = gis.content.search('\"Landsat Multispectral\"', 'Imagery Layer')[0]\n",
    "landsat_item"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "landsat = landsat_item.layers[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def extract_stretch(bandids):\n",
    "    return stretch(extract_band(landsat, bandids),\n",
    "                   stretch_type='PercentClip',\n",
    "                   min_percent=2, \n",
    "                   max_percent=2,\n",
    "                   dra=True, \n",
    "                   gamma=[0.8,0.8,0.8])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from arcgis.geocoding import geocode\n",
    "area = geocode('Cambridge Gulf')[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7e3f856cf0d94d7aa7b8acbc5377219d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MapView(layout=Layout(height='400px', width='100%'), zoom=10.0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m = gis.map(area, 10)\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "m.add_layer(extract_stretch([5, 4, 1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "675b879bce9e4a46b93833f05e7f216e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MapView(layout=Layout(height='400px', width='100%'), zoom=11.0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m2 = gis.map(\"Guelb Er Richat\", 11)\n",
    "m2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "m2.add_layer(extract_stretch([6, 3, 1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6b4c1a055a304a0287f398fbf6742271",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MapView(layout=Layout(height='400px', width='100%'), zoom=10.0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m3 = gis.map(\"Gosses Bluff\", 10)\n",
    "m3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "m3.add_layer(extract_stretch([6, 3, 0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d3bc43e9664b45d6b7ff4b35b8ec4811",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MapView(layout=Layout(height='400px', width='100%'), zoom=11.0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m4 = gis.map(\"Great Exumas, Bahamas\", 11)\n",
    "m4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "m4.add_layer(extract_stretch([5, 3, 0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "54eee94768d04a51be26ec57e64b0d24",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MapView(layout=Layout(height='400px', width='100%'), zoom=10.0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m5 = gis.map(\"Mexico City\", 10)\n",
    "m5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "m5.add_layer(extract_stretch([6, 5, 3]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b4a07def91fc431b9556da364d1954c1",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MapView(layout=Layout(height='400px', width='100%'), zoom=12.0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m6 = gis.map(\"Ash Simasiyah, Saudi Arabia\", 12)\n",
    "m6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "m6.add_layer(extract_stretch([5, 4, 1]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pygeo",
   "language": "python",
   "name": "pygeo"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
